<template>
  <div style="width:500px;">
    <el-form :model="uMsg" label-position="top" label-width="80px">
      <el-form-item label="头像">
        <el-upload class="avatar-uploader" :action="`${apiConfig}/front/User/uploadAvatar`" :show-file-list="false" :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload" :headers="{atoken:userMsg.token, aid:userMsg.objid}">
          <img v-if="uMsg.avatar" :src="uMsg.avatar" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="昵称">
        <el-input v-model="uMsg.nickname"></el-input>
      </el-form-item>
      <el-form-item label="性别">
        <el-select v-model="uMsg.sex" placeholder="请选择">
          <el-option label="男" :value="1"></el-option>
          <el-option label="女" :value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="介绍">
        <el-input type="textarea" :autosize="{ minRows: 3, maxRows: 6}" v-model="uMsg.introduce"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="resetUmsg">立即修改</el-button>
      </el-form-item>
    </el-form>
    <el-form :model="password" label-position="top" label-width="80px">
      <el-form-item label="原密码">
        <el-input type="password" v-model="password.old"></el-input>
      </el-form-item>
      <el-form-item label="新密码">
        <el-input type="password" v-model="password.password"></el-input>
      </el-form-item>
      <el-form-item label="确认密码">
        <el-input type="password" v-model="password.reppassword"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="updateP">修改密码</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { mapMutations, mapGetters } from 'vuex'
import { apiConfig, imgUrlQ } from "@/api/apiTool"
import { resetUserMsg, updatePassword } from "@/api/front/user"
export default {
  data() {
    return {
      uMsg: {},
      password: {
        old: "",
        password: "",
        reppassword: ""
      },
      apiConfig: apiConfig.api,
    }
  },
  mounted() {
    this.uMsg = JSON.parse(JSON.stringify(this.userMsg))
  },
  computed: {
    ...mapGetters({
      userMsg: 'user/getUserMsg',
      userType: 'user/getUserType',
      userLegal: 'user/getUserIsLegal'
    })
  },
  methods: {
    ...mapMutations({
      setUserMsg: 'user/setUserMsg'
    }),
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      if (res.code == 200) {
        this.uMsg.avatar = res.data
        this.$message.success("头像上传成功")
      }
      else {
        this.$message.error(res.msg)
      }
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg' || file.type === 'image/png' || file.type === 'image/jpg' || file.type === 'image/gif';
      const isLt2M = file.size / 1024 / 1024 < 10;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG,JPEG,PNG,GIF 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 10MB!');
      }
      return isJPG && isLt2M;
    },
    resetUmsg() {
      if (this.uMsg.nickname) {
        resetUserMsg(this.uMsg, (res) => {
          if (res.code == 200) {
            this.setUserMsg(this.uMsg)
          }
        }, true)
      }
      else {
        this.$message("请至少填写昵称")
      }
    },
    updateP() {
      if (this.password.old && this.password.password && this.password.reppassword) {
        updatePassword(Object.assign({ objid: this.userMsg.objid }, this.password), (res) => {
          if (res.code == 200) {
            this.password = {
              old: "",
              password: "",
              reppassword: ""
            }
          }
        }, true)
      }
    }
  }
}
</script>

<style lang="less" scoped>
.avatar-uploader .el-upload {
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
  border: 1px dashed #d9d9d9;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>